﻿using System.Collections.Generic;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Primitives;
using OpenRA.Traits;

namespace OpenRA.Mods.Common.Traits.Render
{
	[Desc("TA Special.")]
	public class CircleSelectionDecorationsInfo : SelectionDecorationsBaseInfo, Requires<UnitIsoSelectableInfo>
	{
		public readonly WDist Radius = new WDist(512);
		public readonly int Width = 1;
		public readonly bool RenderExtraOnGround = false;
		public readonly WDist RadiusOnGround = new WDist(256);
		public readonly int WidthOnGround = 1;
		public readonly WDist OffsetOfLine = new WDist(-256);

		public override object Create(ActorInitializer init) { return new CircleSelectionDecorations(init.Self, this); }
	}

	public class CircleSelectionDecorations : SelectionDecorationsBase, IRender
	{
		new readonly CircleSelectionDecorationsInfo info;
		readonly TAUnitSelectable selectable;
		public CircleSelectionDecorations(Actor self, CircleSelectionDecorationsInfo info)
			: base(info)
		{
			this.info = info;
			selectable = self.Trait<TAUnitSelectable>();
		}

		int2 GetDecorationPosition(Actor self, WorldRenderer wr, string pos)
		{
			var bounds = selectable.Bounds(self, wr);
			switch (pos)
			{
				case "TopLeft": return bounds.Vertices[1];
				case "TopRight": return bounds.Vertices[5];
				case "BottomLeft": return bounds.Vertices[2];
				case "BottomRight": return bounds.Vertices[4];
				case "Top": return new int2((bounds.Vertices[1].X + bounds.Vertices[5].X) / 2, bounds.Vertices[1].Y);
				default: return bounds.BoundingRect.TopLeft + new int2(bounds.BoundingRect.Size.Width / 2, bounds.BoundingRect.Size.Height / 2);
			}
		}

		static int2 GetDecorationMargin(string pos, int2 margin)
		{
			switch (pos)
			{
				case "TopLeft": return margin;
				case "TopRight": return new int2(-margin.X, margin.Y);
				case "BottomLeft": return new int2(margin.X, -margin.Y);
				case "BottomRight": return -margin;
				case "Top": return new int2(0, margin.Y);
				default: return int2.Zero;
			}
		}

		protected override int2 GetDecorationOrigin(Actor self, WorldRenderer wr, string pos, int2 margin)
		{
			return wr.Viewport.WorldToViewPx(GetDecorationPosition(self, wr, pos)) + GetDecorationMargin(pos, margin);
		}

		protected override IEnumerable<IRenderable> RenderSelectionBox(Actor self, WorldRenderer wr, Color color)
		{
			yield return new CircleSelectionRenderable(self, info.Radius, color, info.Width);

			// Only render this decoration when above ground
			if (info.RenderExtraOnGround && (self.World.Map.DistanceAboveTerrain(self.CenterPosition) + (info.OffsetOfLine.Length > 0 ? WDist.Zero : info.OffsetOfLine)).Length > 0)
			{
				var groundPosition = self.CenterPosition - new WVec(WDist.Zero, WDist.Zero, self.World.Map.DistanceAboveTerrain(self.CenterPosition));
				yield return new LineAnnotationRenderable(self.CenterPosition + new WVec(WDist.Zero, WDist.Zero, info.OffsetOfLine), groundPosition, info.WidthOnGround, color);
				yield return new CircleAnnotationRenderable(groundPosition, info.RadiusOnGround, info.WidthOnGround, color);
			}
		}

		protected override IEnumerable<IRenderable> RenderSelectionBars(Actor self, WorldRenderer wr, bool displayHealth, bool displayExtra)
		{
			if (!displayHealth && !displayExtra)
				yield break;

			var bounds = selectable.DecorationBounds(self, wr);
			yield return new SelectionBarsAnnotationRenderable(self, bounds, displayHealth, displayExtra);
		}

		IEnumerable<IRenderable> IRender.Render(Actor self, WorldRenderer wr)
		{
			yield break;
		}

		IEnumerable<Rectangle> IRender.ScreenBounds(Actor self, WorldRenderer wr)
		{
			yield return selectable.Bounds(self, wr).BoundingRect;
		}

		public (WDist, int) GetSelectionCircleData() { return (info.Radius, info.Width); }
	}
}
